import scrapy
from DownloadImg.items import DownloadimgItem


class DownloadimgSpider(scrapy.Spider):
    """
    目标：站长素材图片下载
    使用scrapy中自带的ImgesPipeline下载
    """
    name = 'downloadimg'
    # allowed_domains = ['www.xxx.com']
    start_urls = ['https://sc.chinaz.com/tupian/fengjingtupian.html']
    
    # 多页解析
    urls = ['https://sc.chinaz.com/tupian/fengjingtupian_%d.html'%i for i in range(2, 5)]
    urls_index = 0
    def parse(self, response):
        for div in response.xpath('//*[@id="container"]/div'):
            # 这里图片是懒加载 只加载已经显示的图片的src属性 由于我们并没有打开窗口 所以返回空值
            # 没有加载的src属性是在src2里
            # img_url = div.xpath('./div/a/img/@src').extract_first()
            img_url = div.xpath('./div/a/img/@src2').extract_first()
            
            # 带_s的是缩略图 去掉 加上完整的域名得到图片地址
            #https://scpic.chinaz.net/files/pic/pic9/202104/apic32405.jpg
            img_url = 'https:' + img_url.replace('_s','')
            # print(img_url)
            item = DownloadimgItem()
            item['img_url'] = img_url
            
            yield item
        
        if self.urls_index < len(self.urls):
            yield scrapy.Request(self.urls[self.urls_index], callback=self.parse)
            self.urls_index += 1